package mysql

import (
	"Info/model"
	"Info/utils/Timeutils"
	"go.uber.org/zap"
	"time"
)

type CollegeNewsDAO struct {
}

var CollegeNews = new(CollegeNewsDAO)

func (CollegeNewsDAO) Insert(p *model.CollegeNews) (err error) {
	err = db.Create(p).Error
	if err != nil {
		zap.L().Error("[dao mysql news] insert new error ", zap.Error(err))
	}
	return
}

func (CollegeNewsDAO) QueryFirstNews() (*model.CollegeNews, error) {
	news := new(model.CollegeNews)
	err := db.Find(news).Error
	if err != nil {
		zap.L().Warn("[dao mysql news] query new error ", zap.Error(err))
		return nil, err
	}
	return news, nil
}

func (CollegeNewsDAO) QueryLastTime() (time.Time, error) {
	var news1 model.CollegeNews
	var news2 model.CollegeNews
	if err := db.First(&news1).Error; err != nil {
		zap.L().Warn("[dao mysql new] query news error ", zap.Error(err))
	}
	if err := db.Last(&news2).Error; err != nil {
		zap.L().Warn("[dao mysql new] query news error ", zap.Error(err))
	}
	if Timeutils.IsTime1BeforeTime2(news1.NewsDate, news2.NewsDate) {
		return news2.NewsDate, nil
	} else {
		return news1.NewsDate, nil
	}
}
